package com.rupeng.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rupeng.pojo.Segment;
import com.rupeng.service.SegmentService;
import com.rupeng.util.AjaxResult;
import com.rupeng.util.CommonUtils;

@Controller
@RequestMapping("/segment")
public class SegmentController {
	@Autowired
	private SegmentService segmentService;
	@RequestMapping("/list.do")
	public ModelAndView list(Long chapterId) {
		ModelAndView modelAndView = new ModelAndView("segment/list");
		Segment segment = new Segment();
		segment.setChapterId(chapterId);
		List<Segment> segments = segmentService.selectList(segment);
		modelAndView.addObject("segments", segments);
		modelAndView.addObject("chapterId", chapterId);
		return modelAndView;
	}
	@RequestMapping(value="/add.do",method=RequestMethod.GET)
	public ModelAndView addPage(Long chapterId) {
		return new ModelAndView("segment/add", "chapterId", chapterId);
	}
	@RequestMapping(value="/add.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult addSubmit(Long chapterId, String name, Integer seqNum, String description, String videoCode) {
		if (chapterId==null || seqNum==null || CommonUtils.isEmpty(name) || CommonUtils.isEmpty(description) || CommonUtils.isEmpty(videoCode)) {
			return AjaxResult.errorInstance("表单信息不完整");
		}
		Segment segment = new Segment();
		segment.setChapterId(chapterId);
		segment.setName(name);
		if (segmentService.isExisted(segment)) {
			return AjaxResult.errorInstance("该段落名已存在");
		}
		segment = new Segment();
		segment.setChapterId(chapterId);
		segment.setSeqNum(seqNum);
		if (segmentService.isExisted(segment)) {
			return AjaxResult.errorInstance("序号已被占用");
		}
		segment = new Segment();
		segment.setName(name);
		segment.setSeqNum(seqNum);
		segment.setDescription(description);
		segment.setChapterId(chapterId);
		segment.setVideoCode(videoCode);
		segmentService.insert(segment);
		return AjaxResult.successInstance("段落添加成功");
	}
	@RequestMapping("/delete.do")
	public @ResponseBody AjaxResult delete(Long id) {
		segmentService.delete(id);
		return AjaxResult.successInstance("段落已删除");
	}
	@RequestMapping(value="/update.do",method=RequestMethod.GET)
	public ModelAndView updatePage(Long id) {
		Segment segment = segmentService.selectOne(id);
		return new ModelAndView("segment/update", "segment", segment);
	}
	@RequestMapping(value="/update.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult updateSubmit(Long id, String name, Integer seqNum, String description, String videoCode) {
		if (id==null || seqNum==null || CommonUtils.isEmpty(name) || CommonUtils.isEmpty(description) || CommonUtils.isEmpty(videoCode)) {
			return AjaxResult.errorInstance("表单信息不完整");
		}
		long chapterId = segmentService.selectOne(id).getChapterId();
		Segment segment = new Segment();
		segment.setChapterId(chapterId);
		segment.setName(name);
		segment = segmentService.selectOne(segment);
		if (segment!=null && segment.getId()!=id) {
			return AjaxResult.errorInstance("该段落名已存在");
		}
		segment = new Segment();
		segment.setChapterId(chapterId);
		segment.setSeqNum(seqNum);
		segment = segmentService.selectOne(segment);
		if (segment!=null && segment.getId()!=id) {
			return AjaxResult.errorInstance("序号已被占用");
		}
		segment = segmentService.selectOne(id);
		segment.setName(name);
		segment.setSeqNum(seqNum);
		segment.setDescription(description);
		segment.setVideoCode(videoCode);
		segmentService.update(segment);
		return AjaxResult.successInstance("段落修改成功");
	}
}
